package com.xtedu.examproject.dao.impl;

import com.xtedu.examproject.dao.base.ExamQuestionRelationDao;
import com.xtedu.examproject.entity.ExamQuestion;
import com.xtedu.examproject.entity.ExamQuestionRelation;
import com.xtedu.examproject.util.DBUtil;
import com.xtedu.examproject.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/*
 * Author: xzy
 * Date: 2025/10/16
 * Last Modified by: xzy
 * Last Modified date: 2025/10/16
 * Description: TODO - Add file description here
 */
public class ExamQuestionRelationDaoImpl implements ExamQuestionRelationDao {
    @Override
    public int insert(ExamQuestionRelation examQuestionRelation) throws Exception {
        String sql = "insert into exam_question (exam_id,question_id) values (?,?)";
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setInt(1, examQuestionRelation.getExamId());
            ps.setInt(2, examQuestionRelation.getQuestionId());
            return ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    @Override
    public int insert(int examId, int questionId) {
        String sql = "insert into exam_question (exam_id,question_id) values (?,?)";
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setInt(1, examId);
            ps.setInt(2, questionId);
            return ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int update(ExamQuestionRelation examQuestionRelation) throws Exception {
        String sql = "update exam_question set exam_id=?, question_id=? where id=?";
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setInt(1, examQuestionRelation.getExamId());
            ps.setInt(2, examQuestionRelation.getQuestionId());
            ps.setInt(3, examQuestionRelation.getId());
            return ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    @Override
    public int delete(ExamQuestionRelation examQuestionRelation) throws Exception {
        String sql = "delete from exam_question where id=?";
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setInt(1, examQuestionRelation.getId());
            return ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    @Override
    public int deleteById(int id) throws Exception {
        String sql = "delete from exam_question where id=?";
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setInt(1, id);
            return ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    @Override
    public ExamQuestionRelation findById(int id) throws Exception {
        return null;
    }

    @Override
    public List<ExamQuestionRelation> findAll() throws Exception {
        return List.of();
    }

    @Override
    public PageHelper findAllByPage(int pageindex, int pagesize) throws Exception {
        return null;
    }

    @Override
    public PageHelper findPageByRoleIds(int pageindex, int pagesize, int... roleIds) throws Exception {
        return null;
    }

    @Override
    public List<ExamQuestion> findByExamId(int examId) {
        String sql = """
                SELECT e.*,q.type_name,c.course_name FROM exam_question eq
                LEFT JOIN examquestions e ON eq.question_id = e.id
                LEFT JOIN questiontype q ON e.type_id=q.id
                LEFT JOIN courses c ON e.course_id=c.id
                WHERE eq.exam_id=?
                """;
        try (
                Connection conn = DBUtil.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql)
        ) {
            ps.setInt(1, examId);
            ResultSet resultSet = ps.executeQuery();
            List<ExamQuestion> list = new ArrayList<>();
            while (resultSet.next()) {
                ExamQuestion examQuestion = new ExamQuestion();
                examQuestion.setId(resultSet.getInt("id"));
                examQuestion.setTitle(resultSet.getString("title"));
                examQuestion.setOption(resultSet.getString("option"));
                examQuestion.setAnswer(resultSet.getString("answer"));
                examQuestion.setTypeId(resultSet.getInt("type_id"));
                examQuestion.setCourseId(resultSet.getInt("course_id"));
                examQuestion.setState(resultSet.getBoolean("state"));
                examQuestion.setTypeName(resultSet.getString("type_name"));
                examQuestion.setCourseName(resultSet.getString("course_name"));
                list.add(examQuestion);
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
